/*
 * Copyright(C) 2012 Luvina Software Company
 *
 * ListUserManaged.java, May 29, 2012 HaiPD
 */
package net.luvina.manageuser.managedbean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import net.luvina.manageuser.constant.CommonConstant;
import net.luvina.manageuser.dto.UserDetailDto;
import net.luvina.manageuser.logic.MstGroupLogic;
import net.luvina.manageuser.logic.TblUserLogic;
import net.luvina.manageuser.util.CommonUtil;
import net.luvina.manageuser.util.ConfigProperties;
import net.luvina.manageuser.util.Message_ja_JP;

/**
 * ListUser manager bean
 *
 * @author HaiPD
 *
 */
@Component("listUserManaged")
@Scope("session")
public class ListUserManaged extends UserBaseManaged implements Serializable {

	private static final long serialVersionUID = 1L;
	private int groupId;
	private String fullName;
	private List<UserDetailDto> lsUserDetailDto;
	private List<SelectItem> itemGroups;
	private String message;
	private boolean tag = true;
	private String sortByCodeLevel;
	private String sortByEndDate;
	private String sortByFullName;
	private String sortType;
	private int currentPage;
	private List<String> lsPaging;
	private int limit;
	private int offset;
	private int currentSection;
	private int totalRecords;

	/**
	 * Contructor default
	 */
	public ListUserManaged() {
		lsPaging = new ArrayList<String>();
		this.setValueDefault();
		currentSection = CommonUtil.getCurrentSection(currentPage);
	}

	@Autowired
	private TblUserLogic tblUserLogic;

	@Autowired
	private MstGroupLogic mstGroupLogic;

	/**
	 * method initPage method run first JSF
	 *
	 * @param event
	 *            The PhaseEvent object
	 */
	public final void initPage(PhaseEvent event) {
		if (event.getPhaseId() == PhaseId.RENDER_RESPONSE) {
			this.itemGroups = this.mstGroupLogic
					.getListItemGroup(ConfigProperties.getData("default_adm002"));
			this.doSearch();
		}

	}

	/**
	 * method doSearch
	 *
	 * @return The page search
	 */
	public final String doSearch() {
		tag = true;
		limit = CommonUtil.getLimit();
		offset = CommonUtil.getOffset(currentPage, limit);
		this.lsUserDetailDto = this.tblUserLogic.getListUser(offset, limit,
				groupId, fullName, sortType, sortByFullName, sortByCodeLevel,
				sortByEndDate);

		// Begin Paging
		totalRecords = tblUserLogic.getTotalRecords(groupId, fullName);
		lsPaging = CommonUtil.getListPaging(currentPage, totalRecords,
				currentSection);
		// End paging

		// Begin message error
		if (lsUserDetailDto.size() <= 0) {
			tag = false;
			this.message = "";
			message = Message_ja_JP.getString("MSG005");
		}
		// End message error
		this.setValueDefault();
		return CommonConstant.ADM002;
	}

	/**
	 * method doSort
	 *
	 * @return The page search
	 */
	public final String doSort() {
		HttpServletRequest request = (HttpServletRequest) FacesContext
				.getCurrentInstance().getExternalContext().getRequest();
		sortType = request.getParameter("sortType");

		// begin sort by fullname
		if ("sortByFullName".equals(sortType)) {
			sortByFullName = request.getParameter("sortByFullName");
			if ("DESC".equals(sortByFullName)) {
				sortType = "sortByFullName";
				sortByFullName = "DESC";
				return CommonConstant.ADM002;
			}
			if ("ASC".equals(sortByFullName)) {
				sortType = "sortByFullName";
				sortByFullName = "ASC";
				return CommonConstant.ADM002;
			}
		}
		// begin sort by codelevel
		if ("sortByCodeLevel".equals(sortType)) {
			sortByCodeLevel = request.getParameter("sortByCodeLevel");
			if ("DESC".equals(sortByCodeLevel)) {
				sortByCodeLevel = "DESC";
				return CommonConstant.ADM002;
			}
			if ("ASC".equals(sortByCodeLevel)) {
				sortByCodeLevel = "ASC";
				return CommonConstant.ADM002;
			}
		}
		// begin sort by end date
		if ("sortByEndDate".equals(sortType)) {
			sortByEndDate = request.getParameter("sortByEndDate");
			if ("DESC".equals(sortByEndDate)) {
				sortByEndDate = "DESC";
				return CommonConstant.ADM002;
			}
			if ("ASC".equals(sortByEndDate)) {
				sortByEndDate = "ASC";
				return CommonConstant.ADM002;
			}
		}
		return CommonConstant.ADM002;
	}

	/**
	 * method doPaging
	 * @return The page search
	 */
	public final String doPaging() {
		Map<String, String> params = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap();
		String str = params.get("currentPage");
		if (str.equals(">>")) {
			currentSection += 1;
		} else if (str.equals("<<")) {
			currentSection -= 1;
		} else {
			str = str.trim();
			currentPage = Integer.parseInt(str);
		}
		return CommonConstant.ADM002;
	}

	/**
	 * method setValueDetault
	 */
	private void setValueDefault() {
		sortByFullName = "ASC";
		sortByCodeLevel = "ASC";
		sortByEndDate = "DESC";
		currentPage = 1;
	}

	/**
	 * @return the lsUserDetailDto
	 */
	public List<UserDetailDto> getlsUserDetailDto() {
		return lsUserDetailDto;
	}

	/**
	 * @param lsUserDetailDto
	 *            the lsUserDetailDto to set
	 */
	public void setlsUserDetailDto(List<UserDetailDto> lsUserDetailDto) {
		this.lsUserDetailDto = lsUserDetailDto;
	}

	/**
	 * @return the groupId
	 */
	public int getGroupId() {
		return groupId;
	}

	/**
	 * @param groupId
	 *            the groupId to set
	 */
	public void setGroupId(int groupId) {
		this.groupId = groupId;
	}

	/**
	 * @return the itemGroups
	 */
	public List<SelectItem> getItemGroups() {
		return itemGroups;
	}

	/**
	 * @param itemGroups
	 *            the itemGroups to set
	 */
	public void setItemGroups(List<SelectItem> itemGroups) {
		this.itemGroups = itemGroups;
	}

	/**
	 * @return the fullName
	 */
	public String getFullName() {
		return fullName;
	}

	/**
	 * @param fullName
	 *            the fullName to set
	 */
	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param message
	 *            the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * @return the tag
	 */
	public boolean isTag() {
		return tag;
	}

	/**
	 * @param tag
	 *            the tag to set
	 */
	public void setTag(boolean tag) {
		this.tag = tag;
	}

	/**
	 * @return the sortByCodeLevel
	 */
	public String getSortByCodeLevel() {
		return sortByCodeLevel;
	}

	/**
	 * @param sortByCodeLevel
	 *            the sortByCodeLevel to set
	 */
	public void setSortByCodeLevel(String sortByCodeLevel) {
		if ("".equals(sortByCodeLevel)) {
			this.sortByCodeLevel = "DESC";
		}
		this.sortByCodeLevel = sortByCodeLevel;
	}

	/**
	 * @return the sortByEndDate
	 */
	public String getSortByEndDate() {
		return sortByEndDate;
	}

	/**
	 * @param sortByEndDate
	 *            the sortByEndDate to set
	 */
	public void setSortByEndDate(String sortByEndDate) {
		if ("".equals(sortByEndDate)) {
			this.sortByEndDate = "ASC";
		}
		this.sortByEndDate = sortByEndDate;
	}

	/**
	 * @return the sortByFullName
	 */
	public String getSortByFullName() {
		return sortByFullName;
	}

	/**
	 * @param sortByFullName
	 *            the sortByFullName to set
	 */
	public void setSortByFullName(String sortByFullName) {
		this.sortByFullName = sortByFullName;
	}

	/**
	 * @return the sortType
	 */
	public String getSortType() {
		return sortType;
	}

	/**
	 * @param sortType
	 *            the sortType to set
	 */
	public void setSortType(String sortType) {
		this.sortType = sortType;
	}

	/**
	 * @return the currentPage
	 */
	public int getCurrentPage() {
		return currentPage;
	}

	/**
	 * @param currentPage
	 *            the currentPage to set
	 */
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	/**
	 * @return the lsPaging
	 */
	public List<String> getLsPaging() {
		return lsPaging;
	}

	/**
	 * @param lsPaging
	 *            the lsPaging to set
	 */
	public void setLsPaging(List<String> lsPaging) {
		this.lsPaging = lsPaging;
	}

	/**
	 * @return the limit
	 */
	public int getLimit() {
		return limit;
	}

	/**
	 * @param limit
	 *            the limit to set
	 */
	public void setLimit(int limit) {
		this.limit = limit;
	}

	/**
	 * @return the offset
	 */
	public int getOffset() {
		return offset;
	}

	/**
	 * @param offset
	 *            the offset to set
	 */
	public void setOffset(int offset) {
		this.offset = offset;
	}

	/**
	 * @return the currentSection
	 */
	public int getCurrentSection() {
		return currentSection;
	}

	/**
	 * @param currentSection
	 *            the currentSection to set
	 */
	public void setCurrentSection(int currentSection) {
		this.currentSection = currentSection;
	}

	/**
	 * @return the totalRecords
	 */
	public int getTotalRecords() {
		return totalRecords;
	}

	/**
	 * @param totalRecords
	 *            the totalRecords to set
	 */
	public void setTotalRecords(int totalRecords) {
		this.totalRecords = totalRecords;
	}

}
